package com.jsgt.greekfire.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.fhx.core.base.BaseViewModel
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.bean.ActivationBean
import com.jsgt.greekfire.bean.YTWallet
import com.jsgt.greekfire.common.http.Data
import com.jsgt.greekfire.common.http.safeLaunch
import com.jsgt.greekfire.common.util.MD5Util
import com.jsgt.greekfire.repository.WalletRepository

/**
 * author fhx
 * create time 2020/4/24 20:53
 **/
class WalletViewModel1() : BaseViewModel() {
    private val walletRepository = WalletRepository.instance
    val errorLiveData by lazy { MutableLiveData<Any>() }
    val createWalletData by lazy { MutableLiveData<YTWallet>() }
    val importWalletData by lazy { MutableLiveData<YTWallet>() }

    //val walletLiveData = MutableLiveData<WalletInfoBean>()
    val checkSecurityLiveData by lazy { MutableLiveData<Any>() }
    val BindingactivationLiveData by lazy { MutableLiveData<Any>() }

    val createWalletLiveData by lazy { MutableLiveData<Data>() }
    val bindingActivation by lazy { MutableLiveData<Any>() }
    val activationListData by lazy { MutableLiveData<List<ActivationBean>>() }
    val activationTransferData by lazy { MutableLiveData<Any>() }
    val activationData by lazy { MutableLiveData<Any>() }

    /**
     * 创建钱包
     */
    fun createWallet(
        walletName: String,
        securityPwd: String,
        tradingPwd: String,
        utiAddress: String
    ) {
        val params = HashMap<String, Any>()
        params["walletName"] = walletName
        params["securityPassword"] = MD5Util.getMD5(securityPwd)
        params["payPassword"] = MD5Util.getMD5(tradingPwd)
        params["utiAddress"] = utiAddress
        viewModelScope.safeLaunch {
            block = {
                val data = walletRepository.createWallet(params)
                if (data.code == 0) {
                    createWalletData.value = data.result!!
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 创建名称
     */
    fun CreateName(
        walletName: String
    ) {
        val params = HashMap<String, Any>()
        params["userName"] = walletName
        viewModelScope.safeLaunch {
            block = {
                val data = walletRepository.CreateName(params)
                if (data.code == 0) {
//                    createWalletLiveData.value = data.data!!
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 导入钱包
     */
    fun importWallet(
        walletName: String,
        securityPwd: String,
        utiAddress: String
    ) {
        val hashMap = HashMap<String, String>()
        hashMap["walletName"] = walletName
        hashMap["securityPassword"] = securityPwd
        hashMap["address"] = utiAddress

        viewModelScope.safeLaunch {
            block = {
                val data = walletRepository.importWallet(hashMap)
                if (data.code == 0) {
                    importWalletData.value = data.result!!
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                ToastUtils.show(it.message!!)
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 获取钱包信息
     */
    /*fun getWalletInfo() {
        viewModelScope.safeLaunch {
            block = {
                val data = walletRepository.getWalletInfo()
                if (data.code == 0) {
                    walletLiveData.value = data.result
                } else {
                    errorLiveData.value = data
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }*/

    /**
     * 验证钱包密码
     */
    fun checkWalletSecurityPwd(pwd: String) {
        val params = HashMap<String, Any>()
        params["securityPassword"] = MD5Util.getMD5(pwd)
        viewModelScope.safeLaunch {
            block = {
                val data = walletRepository.checkSecurityPassword(params)
                if (data.code == 0) {
                    checkSecurityLiveData.value = data.data
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 绑定激活
     */
    fun bindingActivation(address: String) {
        val params = HashMap<String, Any>()
        params["address"] = address
        viewModelScope.safeLaunch {
            block = {
                val data = walletRepository.Bindingactivation(params)
                if (data.code == 0) {
                    ToastUtils.show(data.msg!!)
                    bindingActivation.value = data.data
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 激活列表
     */
    fun activationList() {
        viewModelScope.safeLaunch {
            block = {
                val data = walletRepository.activationList()
                if (data.code == 0) {
                    activationListData.value = data.result
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 转账激活其他账户
     */
    fun activationTransfer(address: String, amount: String, payPassword: String) {
        val params = HashMap<String, Any>()
        params["addr"] = address
        params["amount"] = amount
        params["payPassword"] = MD5Util.getMD5(payPassword)
        viewModelScope.safeLaunch {
            block = {
                val data = walletRepository.activationTransfer(params)
                if (data.code == 0) {
                    activationTransferData.value = data.data
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 激活
     */
    fun activation(guid: String) {
        val params = HashMap<String, Any>()
        params["guid"] = guid
        viewModelScope.safeLaunch {
            block = {
                val data = walletRepository.activation(params)
                if (data.code == 0) {
                    activationData.value = data.data
                } else {
                    errorLiveData.value = data
                    ToastUtils.show(data.msg!!)
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

}
